package com.proxy.scanandcheck.scaner.http.ip;

import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Date;

import org.apache.log4j.Logger;

import com.proxy.scanandcheck.scaner.valueobject.IpAddress;

public class Scanner {
	
	private static final Logger LOGGER = Logger.getLogger(Scanner.class);
	
    private IpAddress ipAddress;

    public Scanner(IpAddress ipAddress)
    {
        this.ipAddress = ipAddress;
    }

    public  void scan()
    {
        try
        {
            SocketAddress sa = new InetSocketAddress(
            		this.ipAddress.getAddress(),
            		this.ipAddress.getPort());
            
            this.ipAddress.setUpdateTime(new Date());
            
            Socket socket = new Socket();
            socket.connect(sa, 2000);

            /*LOGGER.info("-------- Port in use: " + 
        			this.ipAddress.getAddress() + 
        			":" + this.ipAddress.getPort());*/

            socket.close();
            
            this.ipAddress.setValid(Boolean.TRUE);
        }
        catch (Exception e)
        {
            /*System.out.println("Port not in use: " + 
            			this.ipAddress.getAddress() + 
            			":" + this.ipAddress.getPort());*/
            this.ipAddress.setValid(Boolean.FALSE);
        }
    }

}